package sicnu.cs.aps.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import sicnu.cs.aps.common.util.CollectionUtil;
import sicnu.cs.aps.domain.dto.AnswerKvDto;
import sicnu.cs.aps.domain.dto.QuAndAnDto;
import sicnu.cs.aps.domain.entity.ActivityForm;
import sicnu.cs.aps.domain.entity.ActivityFormQuestion;
import sicnu.cs.aps.domain.entity.ActivityFormStuAnswer;
import sicnu.cs.aps.domain.vo.TeamInfoVo;
import sicnu.cs.aps.mapper.ActivityFormMapper;
import sicnu.cs.aps.mapper.ActivityFormQuestionMapper;
import sicnu.cs.aps.mapper.ActivityFormStuAnswerMapper;
import sicnu.cs.aps.mapper.ActivityTeamMapper;
import sicnu.cs.aps.service.IActivityFormService;
import sicnu.cs.aps.service.IActivityTeamService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author lucker
 * @date 2022/6/5 17:58
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ActivityFormServiceImpl extends ServiceImpl<ActivityFormMapper, ActivityForm> implements IActivityFormService {

    private final ActivityFormMapper formMapper;
    private final ActivityFormStuAnswerMapper formStuAnswerMapper;
    private final ActivityTeamMapper activityTeamMapper;
    private final ActivityFormQuestionMapper formQuestionMapper;

    /**
     * 查询问题信息
     *
     * @param activityTeams 小队列表
     * @return 问题及答案列表
     * @author walnut
     * @description 查询问题信息
     * @date 2022/6/7 12:01
     */
    @Override
    public List<AnswerKvDto> listQaInTeams(List<TeamInfoVo> activityTeams, List<Long> teamIds) {
        List<Long> activityIds = activityTeams.stream().map(TeamInfoVo::getActivityId).collect(Collectors.toList());
        List<ActivityForm> activityForms = formMapper.selectBatchIds(activityIds);
        List<QuAndAnDto> quAndAnDtoList = new ArrayList<>();
//        activityForms
//                .forEach(activityForm -> CollectionUtil.separateString(activityForm.getQuestions())
//                        .forEach(qId -> quAndAnDtoList.add(QuAndAnDto.builder()
//                                .aId(activityForm.getActivityId())
//                                .qId(Long.valueOf(qId)).build())));
        activityForms
                .forEach(activityForm ->
                        {
                            List<String> strings = CollectionUtil.separateString(activityForm.getQuestions());
                            quAndAnDtoList.add(QuAndAnDto.builder().aId(activityForm.getActivityId())
                                    .qId(new ArrayList<>(
                                            formQuestionMapper.selectBatchIds(strings)
                                                    .stream()
                                                    .map(ActivityFormQuestion::getQuestion)
                                                    .collect(Collectors.toList())
                                    )).build());
                        }
                );


        List<ActivityFormStuAnswer> activityFormStuAnswers = formStuAnswerMapper.selectBatchIds(teamIds);
        List<AnswerKvDto> answerKvDtoList = new ArrayList<>();


        activityTeams.forEach(stuA -> {
            answerKvDtoList.add(AnswerKvDto.builder()
                    .teamId(stuA.getId())
                    .questions(quAndAnDtoList.stream()
                            .filter( st-> st.getAId().equals(stuA.getActivityId()))
                            .map(QuAndAnDto::getQId)
                            .findFirst().orElse(null))
                    .answers(CollectionUtil.separateString(activityFormStuAnswers.stream()
                            .filter(activityFormStuAnswer -> activityFormStuAnswer.getTeamId().equals(stuA.getId()))
                            .map(ActivityFormStuAnswer::getAnswers)
                            .findFirst().orElse(null)))
                    .build());
        });

//        activityFormStuAnswers.forEach(activityFormStuAnswer ->
//                {
//                    AtomicInteger i = new AtomicInteger(0);
//                    Long activityId = activityTeamMapper.selectByTeamId(activityFormStuAnswer.getTeamId()).getActivityId();
//                    List<String> questions = quAndAnDtoList.stream().filter(ql -> ql.getAId().equals(activityId)).map(QuAndAnDto::getQId).findFirst().orElse(new ArrayList<>());
//                    List<String> qText = formQuestionMapper.selectBatchIds(questions).stream().map(ActivityFormQuestion::getQuestion).collect(Collectors.toList());
//                    CollectionUtil.separateString(activityFormStuAnswer.getAnswers())
//                            .forEach(answer -> {
//                                        answerKvDtoList.add(AnswerKvDto.builder().teamId(activityFormStuAnswer.getTeamId()).question(qText.get(i.get())).answers(answer).build());
//                                        i.getAndIncrement();
//                                    }
//                            );
//                }
//        );

        return answerKvDtoList;
    }

}
